package com.cms.tags;

/**
 * <p>Title: ECCM</p>
 *
 * <p>Description: Enterprise Collaboration, Customer, and Resources
 * Management</p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: eConage Software</p>
 *
 * @author not attributable
 * @version 1.0
 */
import java.io.IOException;
import java.util.ResourceBundle;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.log4j.Logger;
import org.apache.taglibs.i18n.ResourceHelper;

public class PaginationBarTag extends TagSupport {

	private final static Logger logger = Logger
			.getLogger(PaginationBarTag.class);

	int itemTotal;
	int itemPerPage;
	int currentPage;
	int styletype;

	public void setItemTotal(int itemTotal) {
		this.itemTotal = itemTotal;
	}

	public void setItemPerPage(int itemPerPage) {
		this.itemPerPage = itemPerPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	
	public void setStyletype(int styletype) {
		this.styletype = styletype;
	}
	
	public int doStartTag() throws JspException {
		try {

			ResourceBundle bundle = ResourceHelper.getBundle(pageContext);

			JspWriter out = pageContext.getOut();
			// int pageTotal = itemTotal/itemPerPage + 1;
			// int pageTotal = (int)((double)itemTotal/itemPerPage + 0.5);
			int pageTotal = 0;
			double tempnum = (double) itemTotal / itemPerPage;
			if ((tempnum - (int) tempnum) == 0)
				pageTotal = (int) tempnum;
			else
				pageTotal = (int) tempnum + 1;
			
			String style="";
			switch (styletype) {
			case 1:
				style = "m-page m-page-sr m-page-sm";
				break;

			default:
				style = "m-page m-page-sr m-page-sm";
				break;
			}
					
			out.print("<nav class='"+style+"'>");
			if (currentPage > 1) {
				out.print("<a href=\"javascript:getListByPageNum('");
				out.print(currentPage - 1);
				out.print("')\" class='pageprv' >");
				out.print(bundle.getString("prev_page"));
				out.print("</a>");
			}
			
			int startpage = 1;
			int endpage = pageTotal;
			
			if(pageTotal>9){
				startpage = (currentPage - 8)>1?(currentPage - 8):1;
				endpage = (startpage + 8)<pageTotal-1?(startpage + 8):pageTotal-1;
			}
			for (int i = startpage; i < endpage; i++) {
					out.print("<a class='"+((i == currentPage)?"z-crt":"")+"' href=\"javascript:getListByPageNum('");
					out.print(i);
					out.print("')\">");
					out.print(i);
					out.print("</a>&nbsp;");
			}
            
			if(endpage!=pageTotal-1&&pageTotal>9){
				out.println("<i>...</i>");
			}
			
			out.print("<a href=\"javascript:getListByPageNum('");
			out.print(pageTotal);
			out.print("')\">");
			out.print(pageTotal);
			out.print("</a>&nbsp;");

			if (currentPage < pageTotal) {
				out.print("<a href=\"javascript:getListByPageNum('");
				out.print(currentPage + 1);
				out.print("')\" class='pagenxt'>");
				out.print(bundle.getString("next_page"));
				out.print("</a>");
			}
            out.print("<a href='#'>\u7b2c "+currentPage+"&nbsp;/&nbsp;<span style='font-weight:bold;'>"+pageTotal+"</span> \u9875&nbsp;&nbsp;");
            out.print("\u603b\u8bb0\u5f55\u6570:<span style='font-weight:bold;color:red'> "+itemTotal+" </span>\u6761</a>");
			out.print("</nav>");

		} catch (IOException ioe) {
			throw new JspException("Error: IOException while writing to client"
					+ ioe.getMessage());
		}

		return SKIP_BODY;
	}

	public int doEndTag() throws JspException {
		return EVAL_PAGE;
	}
}
